#include "engine.h"

engine::engine()
{
	quit = false;
	s = main;
	background = NULL;
	screen = NULL;
	audio = NULL;
}

bool engine::Init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
		return false;

	if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096))
		return false;

	screen = SDL_SetVideoMode(600, 480, 32, SDL_SWSURFACE);

	SDL_WM_SetCaption("Alpha Engine", NULL);

	return true;
}

int engine::getState()
{
	return s;
}

void engine::setState(int i)
{

}

void engine::apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, clip, destination, &offset);
}

SDL_Surface* engine::load_image(std::string filename)
{
	SDL_Surface *loadedImage = NULL;
	SDL_Surface *optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());

	if(loadedImage != NULL)
	{
		optimizedImage = SDL_DisplayFormat(loadedImage);

		SDL_FreeSurface(loadedImage);
	}

	//Here be dinosaur code
	/*if(optimizedImage != NULL)
	{
		Uint32 colorkey = SDL_MapRGB(optimizedImage->format,255,255,255);
		SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
	}*/


	return optimizedImage;
}

bool engine::CollisionCheck(SDL_Rect A, SDL_Rect B)
{
	int leftA, leftB;
	int rightA, rightB;
	int topA, topB;
	int bottomA, bottomB;

	leftA = A.x;
	rightA = A.x + A.w;
	topA = A.y;
	bottomA = A.y + A.h;

	leftB = B.x;
	rightB = B.x + B.w;
	topB = B.y;
	bottomB = B.y + B.h;

	if( bottomA <= topB )
	{
		return false;
	}

	if( topA >= bottomB )
	{
		return false;
	}

	if( rightA <= leftB )
	{
		return false;
	}

	if( leftA >= rightB )
	{
		return false;
	}

	return true;
}

bool engine::CollisionCheckPP(std::vector<SDL_Rect> A, std::vector<SDL_Rect> B)
{

	for(int x = 0; x < A.size(); x++)
	{

		for(int y = 0; y < B.size(); y++)
		{
			if(CollisionCheck(A[x], B[y]))
				return true;
		}
	}

	return false;
}

bool engine::CollisionCheckCirc()
{
	//I don't really do anything at the moment but I am not needed currently
	//I am sad and unloved by my creator

	//TODO: Implement this at some point too whether or not you need it
	//Implementation of this will require another class of type circle
	//with attributes x, y, and radius and a few other funtions that
	//are used to calculate trajectory.

	return false;
}

/*
 * Sets the transparency color to that of the color of the input pixel
 */
void engine::setColorKeyT(SDL_Surface *surface, Uint32 pixel)
{
	if(surface != NULL)
	{
		SDL_SetColorKey(surface, SDL_SRCCOLORKEY, pixel);
	}
}

/*
 * Returns the pixel at location x, y in the image
 */
Uint32 engine::getPixel(SDL_Surface *surface, int x, int y)
{
	Uint32 *pixels = (Uint32 *)surface->pixels;

	return pixels[(y * surface->w) + x];
}

/*
 * Very powerful function
 * use with caution
 */
void engine::setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
	Uint32 *pixels = (Uint32 *)surface->pixels;

	pixels[(y * surface->w) + x] = pixel;
}

void engine::printS(SDL_Surface *screen, float x, float y, std::string s)
{
	//Todo: Implement string printing
}
